package com.study.thread.die;

/**
 * wait使用不当造成锁没释放-死锁：
 *
 */
public class DieLockWaitDemo {
    private static Object lock = new Object();
    private static Object lock1 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "：开始运行");
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + "：lock被锁定");
                synchronized (lock1) {
                    System.out.println(Thread.currentThread().getName() + "：lock1被锁定");
                    System.out.println(Thread.currentThread().getName() + "：开始等待lock1");
                    try {
                        lock1.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "：结束");
            }
        }).start();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "：开始运行");
            synchronized (lock1) {
                lock1.notify();
                System.out.println(Thread.currentThread().getName() + "：lock1被锁定");
            }
            System.out.println(Thread.currentThread().getName() + "：lock1已释放");
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + "：lock被锁定");
            }
        }).start();
    }
}
